package com.yc.controller.xcx;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.io.FileUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;

import com.yc.controller.weixin.GenericController;
import com.yc.entity.Page;
import com.yc.entity.xcx.PickerObject;
import com.yc.service.xcx.sufferer.ISuffererService;
import com.yc.service.xcx.user.ISessionManagerService;
import com.yc.util.PageData;
import com.yc.util.ReturnModel;


@Controller
@RequestMapping("/app/sufferer")
public class ESuffererController extends GenericController {
	
	@Resource
	private ISuffererService suffererService;
	
	// 会话管理
	@Resource
	private ISessionManagerService sessionManagerService;

	
	/**
	 * 添加用户病历
	 * @param response
	 * @throws Exception 
	 */
	@RequestMapping(value = "/addSufferer")
    public void addSufferer(MultipartFile picture1,HttpServletResponse response, HttpServletRequest request)  {
        	
			PageData pd = new PageData();
			pd = this.getPageData();
			
			ReturnModel returnModel = new ReturnModel();
			returnModel.setResult(true);
        	returnModel.setReason(null);
        	returnModel.setDatum(null);
        	
			try {
				
				String savePath = request.getSession().getServletContext().getRealPath("/uploadFiles/xcx/");
		
				String filename = picture1.getOriginalFilename();
				String fullname = savePath + File.separator +filename ;
				FileUtils.copyInputStreamToFile(picture1.getInputStream(), new File(fullname));
				pd.put("3TDSESSION", pd.getString("AccessToken"));
				PageData item = sessionManagerService.findById(pd);
				String openid = item.getString("OPENID");
				
				pd.put("SUFFERER_ID", this.get32UUID());
				pd.put("USER_ID", openid);
				pd.put("STATUS", "A");
				pd.put("PIC", filename);
				
				suffererService.save(pd);
				
				
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				returnModel.setResult(false);
	        	returnModel.setReason(e.getMessage());
	        	returnModel.setDatum(null);
			}

            renderString(response, returnModel);
    }
	
	
	/**
	 * 编辑用户病历
	 * @param response
	 * @throws Exception 
	 */
	@RequestMapping(value = "/editSufferer")
    public void editSufferer(HttpServletResponse response, HttpServletRequest request)  {
        	
			PageData pd = new PageData();
			pd = this.getPageData();
			
			ReturnModel returnModel = new ReturnModel();
			returnModel.setResult(true);
        	returnModel.setReason(null);
        	returnModel.setDatum(null);
        	
			try {
				
				PageData entity = suffererService.findById(pd);
				entity.put("PHONE", pd.getString("PHONE"));
				entity.put("SICKHISTORY", pd.getString("SICKHISTORY"));
				
				suffererService.edit(entity);
		
				
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				returnModel.setResult(false);
	        	returnModel.setReason(e.getMessage());
	        	returnModel.setDatum(null);
			}

            renderString(response, returnModel);
    }
	
	/**
	 * 添加用户病历
	 * @param response
	 * @throws Exception 
	 */
	@RequestMapping(value = "/getSuffererList")
    public void getSuffererList(HttpServletResponse response, HttpServletRequest request)  {
        	
			PageData pd = new PageData();
			pd = this.getPageData();
			
			ReturnModel returnModel = new ReturnModel();
			returnModel.setResult(true);
        	returnModel.setReason(null);
        	
        	
			try {
				
				
				pd.put("3TDSESSION", pd.getString("AccessToken"));
				PageData item = sessionManagerService.findById(pd);
				String openid = item.getString("OPENID");
				
				pd.put("USER_ID", openid);
				pd.put("keywords", null);
				
				List<PageData> list = suffererService.getSuffererList(pd);
				returnModel.setDatum(list);
				
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				returnModel.setResult(false);
	        	returnModel.setReason(e.getMessage());
	        	returnModel.setDatum(null);
			}

            renderString(response, returnModel);
    }
	
	
		@RequestMapping(value="/getList")
		public void getList(HttpServletResponse response)throws Exception{
			
			PageData pd = new PageData();
			pd = this.getPageData();
			
			ReturnModel returnModel = new ReturnModel();
			returnModel.setResult(true);
        	returnModel.setReason(null);
        	PickerObject obj =new PickerObject();
			
			List<String> ids = new ArrayList<String>();
			List<String>  values = new ArrayList<String>();
			ids.add("0");
			values.add("请选择患者");
			
			try {
				
				pd.put("3TDSESSION", pd.getString("AccessToken"));
				PageData item1 = sessionManagerService.findById(pd);
				String openid = item1.getString("OPENID");
				
				pd.put("USER_ID", openid);
				pd.put("keywords", null);
				
				List<PageData> list = suffererService.getSuffererList(pd);
				
				for(PageData item :list){
					ids.add(item.getString("SUFFERER_ID").toString());
					values.add(item.getString("SUFFERERNAME").toString());
				}
				
				obj.setIds(ids);
				obj.setValues(values);
				
				returnModel.setResult(true);
				returnModel.setReason(null);
				returnModel.setDatum(obj);
				
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				returnModel.setResult(false);
	        	returnModel.setReason(e.getMessage());
	        	returnModel.setDatum(null);
			}

			this.renderString(response, returnModel);	
			
		}
	
	/**
	 * 删除用户病历
	 * @param response
	 * @throws Exception 
	 */
	@RequestMapping(value = "/deleteSufferer")
    public void deleteSufferer(HttpServletResponse response, HttpServletRequest request)  {
        	
			PageData pd = new PageData();
			pd = this.getPageData();
			
			ReturnModel returnModel = new ReturnModel();
			returnModel.setResult(true);
        	returnModel.setReason(null);
        	
			try {
				
				pd.put("3TDSESSION", pd.getString("AccessToken"));
				
				suffererService.delete(pd);
				returnModel.setDatum(null);
				
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				returnModel.setResult(false);
	        	returnModel.setReason(e.getMessage());
	        	returnModel.setDatum(null);
			}

            renderString(response, returnModel);
    }
	
	
	/**
	 * 删除用户病历
	 * @param response
	 * @throws Exception 
	 */
	@RequestMapping(value = "/getSufferer")
    public void getSufferer(HttpServletResponse response, HttpServletRequest request)  {
        	
			PageData pd = new PageData();
			pd = this.getPageData();
			
			ReturnModel returnModel = new ReturnModel();
			returnModel.setResult(true);
        	returnModel.setReason(null);
        	
			try {
				
				pd.put("3TDSESSION", pd.getString("AccessToken"));
				PageData item = suffererService.findById(pd);
				returnModel.setDatum(item);
				
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				returnModel.setResult(false);
	        	returnModel.setReason(e.getMessage());
	        	returnModel.setDatum(null);
			}

            renderString(response, returnModel);
    }
	
	

}
